<?php

class UploadController extends Controller {

    public function imageAction() {
        if (!isset($_FILES['file'])) {
            Response::ajax(NORMAL_ERROR, '参数传递有误');
        }
        $file = $_FILES['file'];
        $fileUpload = $this->instance('FileUpload');
        if (!$fileUpload->uploadFileError($file['error'])) {
            Response::ajax(NORMAL_ERROR, $fileUpload->getErrorInfo());
        }
        $file_name = uniqid('qx');
        $file_dir = '/upload/' . date('Ym') . '/';
        $des_dir = APP_ROOT . $file_dir;
        File::makeDir($des_dir);
        $des_file = $des_dir . $file_name;

	    //add
        if (strpos($file['type'], 'php')) {
            Response::ajax(SYSTEM_ERROR, '文件上传失败！请重新上传');
        }

        if (!$fileUpload->setLimitType(array('jpg', 'jpeg', 'gif', 'png'))->setLimitSize(4096000)->moveFile($file, $des_file)) {
            Response::ajax(SYSTEM_ERROR, $fileUpload->getErrorInfo());
        }
        $image_url = Request::getServerName() . Router::getBaseUrl() . $file_dir . $file_name. '.' . strtolower(substr(strrchr($file['name'], '.'), 1));
        Response::ajax(200, '图片上传成功！', array('image' => $image_url));
    }
    
    public function videoAction() {
        if (!isset($_FILES['video'])) {
            Response::ajax(NORMAL_ERROR, '参数传递有误');
        }
        $file = $_FILES['video'];
        $fileUpload = $this->instance('FileUpload');
        if (!$fileUpload->uploadFileError($file['error'])) {
            Response::ajax(NORMAL_ERROR, $fileUpload->getErrorInfo());
        }

        //add
        if (strpos($file['type'], 'php')) {
            Response::ajax(SYSTEM_ERROR, '文件上传失败！请重新上传');
        }

        $uniqName = uniqid('w');
        $fileName = $uniqName . '.' . strtolower(substr(strrchr($file['name'], '.'), 1));
        
        $fileDir = '/upload/' . date('Ym') . '/';
        $desDir = APP_ROOT . $fileDir;
        File::makeDir($desDir);
        $desFile = $desDir . $fileName;
        $imageFile = $desDir . $uniqName.'.jpg';
        $gifFile = $desDir. $uniqName.'.gif';
        if (!move_uploaded_file($file['tmp_name'], $desFile)) {
            Response::ajax(SYSTEM_ERROR, '文件上传失败！请重新上传');
        }
        $imageUrl = $gifUrl = '';
        try{
            //生成gif图片
            $output = '';
//            @exec("/sbin/ffmpeg -t 3 -ss 00:00:01 -i '{$desFile}' '{$gifFile}'", $output);
            $gifUrl = Request::getServerName() . Router::getBaseUrl() . $fileDir . $uniqName.'.gif';
            //生成jpg图片
//            @exec("/sbin/ffmpeg -i '{$desFile}' -y -f image2 -ss 1 -vframes 1  '{$imageFile}'", $output);
            $imageUrl = Request::getServerName() . Router::getBaseUrl() . $fileDir . $uniqName.'.jpg';
        } catch (Exception $ex) {
            $imageUrl = 'default.jpg';
            $gifUrl = 'default.gif';
            Log::write('视频转码失败:'.$ex, 'WARN');
        }
        $videoUrl = Request::getServerName() . Router::getBaseUrl() . $fileDir . $fileName;
        
        Response::ajax(200, '视频上传成功！', [
            'videoUrl' => $videoUrl,
            'imageUrl' => $imageUrl,
            'gifUrl' => $gifUrl
        ]);
    }

    public function base64Action() {
//        Log::write(serialize($_POST), 'Error', 'POST');
//        Log::write(serialize(file_get_contents('php://input')), 'Error', 'input');
        //add
	if (false !== strpos(Request::post('image'), 'php')) {
            Response::ajax(SYSTEM_ERROR, '文件上传失败！请重新上传');
        }
	
	$base64Content = base64_decode(Request::post('image'));
        $type = 'jpg';
//        if (!$type || !in_array($type, array('jpg', 'jpeg', 'gif', 'png'))) {
//            Response::ajax(-10001, '上传图片类型有误');
//       }

        $fileTmp = uniqid('qx');
        $dirTmp = '/upload/' . date('Ym') . '/';
        $desDir = APP_ROOT . $dirTmp;
        File::makeDir($desDir);
        $desFile = $desDir . $fileTmp . '.' . $type;
        if (file_put_contents($desFile, $base64Content)) {
            $urlFile = Request::getServerName() . Router::getBaseUrl() . $dirTmp . $fileTmp . '.' . $type;
            Response::ajax(200, '图片上传成功！', array('image' => $urlFile));
        }
        Response::ajax(-1, '图片不是base64编码,上传失败');
    }

}
